<!doctype html>
<!--
Copyright 2018 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="description" content="Sample illustrating the use of WebAssembly">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>WebAssembly Sample</title>

    <!-- Add to homescreen for Chrome on Android -->
    <meta name="mobile-web-app-capable" content="yes">
    <link rel="icon" sizes="192x192" href="../images/touch/chrome-touch-icon-192x192.png">

    <!-- Add to homescreen for Safari on iOS -->
    <meta name="apple-mobile-web-app-title" content="WebAssembly Sample">

    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" href="../images/apple-touch-icon-precomposed.png">

    <!-- Tile icon for Win8 (144x144 + tile color) -->
    <meta name="msapplication-TileImage" content="../images/touch/ms-touch-icon-144x144-precomposed.png">
    <meta name="msapplication-TileColor" content="#3372DF">

    <link rel="icon" href="../images/favicon.ico">

    <link rel="stylesheet" href="../styles/main.css">
  </head>

  <body>
    <h1>Web Assembly</h1>

    <p>Available in <a href="https://www.chromestatus.com/features/5453022515691520">Chrome 57+</a></p>

    <!-- // [START code-block] -->
    <p>
      WebAssembly allows you to compile code from other languages like C or C++
      to run on the web at native speed.
    </p>
    <div class="output">
        <pre id="log"></pre>
    </div>
    <script>
        async function loadImage(src) {
          // Load image
          const imgBlob = await fetch(src).then(resp => resp.blob());
          const img = await createImageBitmap(imgBlob);
          // Make canvas same size as image
          const canvas = document.createElement('canvas');
          canvas.width = img.width;
          canvas.height = img.height;
          // Draw image onto canvas
          const ctx = canvas.getContext('2d');
          ctx.drawImage(img, 0, 0);
          return ctx.getImageData(0, 0, img.width, img.height);
        }
    </script>
    <!-- // [START code-block] -->
    <script src="image.js"></script>
    <script>
    "use strict";

    Module.onRuntimeInitialized = async _ => {
      // Create wrapper functions for all the exported C functions
      const api = {
        version: Module.cwrap('version', 'number', []),
        create_buffer: Module.cwrap('create_buffer', 'number', ['number', 'number']),
        destroy_buffer: Module.cwrap('destroy_buffer', '', ['number']),
        encode: Module.cwrap('encode', '', ['number', 'number', 'number', 'number']),
        free_result: Module.cwrap('free_result', '', ['number']),
        get_result_pointer: Module.cwrap('get_result_pointer', 'number', []),
        get_result_size: Module.cwrap('get_result_size', 'number', []),
      };

      const image = await loadImage('image.jpg');
      const p = api.create_buffer(image.width, image.height);
      Module.HEAP8.set(image.data, p);
      api.encode(p, image.width, image.height, 100);
      const resultPointer = api.get_result_pointer();
      const resultSize = api.get_result_size();
      const resultView = new Uint8Array(Module.HEAP8.buffer, resultPointer, resultSize);
      const result = new Uint8Array(resultView);
      api.free_result(resultPointer);
      api.destroy_buffer(p);

      const blob = new Blob([result], {type: 'image/webp'});
      const blobURL = URL.createObjectURL(blob);
      const img = document.createElement('img');
      img.src = blobURL;
      document.querySelector('.output').appendChild(img)
    };
    </script>
    <!-- // [END code-block] -->

    <script>
      /* jshint ignore:start */
      (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
      })(window,document,'script','//www.google-analytics.com/analytics.js','ga');
      ga('create', 'UA-53563471-1', 'auto');
      ga('send', 'pageview');
      /* jshint ignore:end */
    </script>
    <!-- Built with love using Web Starter Kit -->
  </body>
</html>
